from dopplershift.core.app.cline import CommandLineApp
import cmd
import socket
import json
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/dopplershift_5000")

def receiving():
    buffer = ''
    while True:
        rec = s.recv(1)
        buffer = buffer + rec
        if buffer[-2:] == '\r\n': break
    return json.loads(buffer)
        
def sending(obj):
    return s.send(json.dumps(obj)+"\r\n")
#class DSCtl(CommandLineApp):
#    _config_file

class DSCtlShell(cmd.Cmd):
    '''
    Dopplershift collector command line interpretor
    '''
    prompt = 'collector>'
    intro = 'Dopplershift collector command processor interpretor'
    
    ruler = '-'
    def do_status(self, line):
        '''
        get server status
        '''
        sending({"command": "status"})
        obj = receiving()
        print obj
    def do_reload(self, line):
        '''
        Reloading collectors templates
        '''
        sending({"command": "reload"})
        obj = receiving()
        print obj
    def do_EOF(self, line):
        return self.do_quit(line)
    def do_quit(self, line):
        '''
        Quit from command processor
        '''
        return True

if __name__ == '__main__':
    DSCtlShell().cmdloop()